﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
namespace DataModel
{
    public abstract class DataModelBase:INotifyPropertyChanged, INotifyDataErrorInfo,IDataErrorInfo
    {
        #region INotifyPropertyChanged Members
        protected virtual void PropertyChangedMethod(string PropertyName, object PropertyValue)
        {

        }

        protected void OnPropertyChanged(string PropertyName)
        {
            PropertyChangedMethod(PropertyName, this.GetType().GetProperty(PropertyName).GetGetMethod().Invoke(this, null));
            foreach (var item in PropertyChangedHandlers)
            {
                try
                {
                    item.Value.Send((o) =>
                        {
                            item.Key(this, new PropertyChangedEventArgs(PropertyName));
                        }, null);
                }
                catch (Exception)
                {
                    item.Key(this, new PropertyChangedEventArgs(PropertyName));
                }
            }
        }

        Dictionary<PropertyChangedEventHandler, SynchronizationContext> PropertyChangedHandlers = new Dictionary<PropertyChangedEventHandler, SynchronizationContext>();
        public event PropertyChangedEventHandler PropertyChanged { 
            add 
            {
                PropertyChangedHandlers.Add(value, SynchronizationContext.Current);
            } 
            remove
            {
                PropertyChangedHandlers.Remove(value);
            } 
        }

        #endregion



        #region IRevertibleChangeTracking Members
        protected void SetRevertValue(string PropertyName, object OldValue)
        {
            //if(!IsNew)
            //    ChangeTracking.SetRevertValue(this, PropertyName, OldValue);
        }
        protected void SetComitAction(string PropertyName, object NewValue, Action<DataModelBase, string, object> ComitPropertyValueAction)
        {
            if(!IsNew)
                ChangeTracking.SetComitAction(this, PropertyName, NewValue, ComitPropertyValueAction);
        }

        public bool IsNew { get; private set; }
        /// <summary>
        /// Save this object as a new item
        /// </summary>
        protected abstract void ComitNewObject();

        public void NewObject()
        {
            IsNew = true;
            ChangeTracking.SetComitNewAction(this, "New Item", null, (DM, PN, PV) => 
                {
                    IsNew = false;
                    ComitNewObject();
                });
        }
        #endregion

        #region INotifyDataErrorInfo Members

        protected void AddMessage(ErrorMessage EM)
        {
            if (Messages.Contains(EM))
            {
                Messages.Remove(EM);
                Messages.Add(EM);
            }
            else
            {
                Messages.Add(EM);
                ChangeTracking.AddError();
            }
            if (ErrorsChanged != null)
            {
                ErrorsChanged(this, new DataErrorsChangedEventArgs(EM.propertyName));
            }
        }
        protected void RemoveMessage(ErrorMessage EM)
        {
            if (Messages.Contains(EM))
            {
                Messages.Remove(EM);
                ChangeTracking.RemoveError();
            }
            if (ErrorsChanged != null)
            {
                ErrorsChanged(this, new DataErrorsChangedEventArgs(EM.propertyName));
            }
        }

        List<ErrorMessage> Messages = new List<ErrorMessage>();
        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

        public System.Collections.IEnumerable GetErrors(string propertyName)
        {
            return Messages.Where((EM) => EM.propertyName == propertyName).Select((EM) => EM.GetMessage);
        }

        public bool HasErrors
        {
            get { return Messages.Any(); }
        }

        #endregion

        #region IDataErrorInfo Members

        public string Error
        {
            get 
            {
                if (!Messages.Any())
                    return "";
                return Messages.Select((EM) => EM.GetMessage).Aggregate((S1, S2) => S1 + Environment.NewLine + S2);
            }
        }

        public string this[string columnName]
        {
            get 
            { 
                if (!Messages.Any((EM)=>EM.propertyName == columnName))
                    return "";
                return Messages.Where((EM) => EM.propertyName == columnName).Select((EM) => EM.GetMessage).Aggregate((S1, S2) => S1 + Environment.NewLine + S2);
            }
        }

        #endregion
    }

    public static class ReflectionHelper
    {
        public static object GetPropertyValue(this object o, string PropertyName)
        {
            return o.GetType().GetProperty(PropertyName).GetGetMethod().Invoke(o, null);
        }
    }
}
